package com.zl.learning.json.gson;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class UserTypeAdapter implements JsonSerializer<User> {
    private Gson gson = new Gson();

    @Override
    public JsonElement serialize(User user, Type type, JsonSerializationContext context) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getId());
        map.put("userName", user.getName());
        map.put("userPassword", user.getPasswd());
        map.put("classRoom",user.getClassRoom());
        return gson.fromJson(gson.toJson(map), JsonObject.class);
    }

}
